import React from 'react';
import { ISurveyHook } from '../../../utils/Survey/SurveyHook';
import FormAloneDate from '../FormAloneDate/FormAloneDate';
import FormCheckbox from '../FormCheckbox/FormCheckbox';
import FormCount from '../FormCount/FormCount';
import FormDate from '../FormDate/FormDate';
import FormDateTime from '../FormDateTime/FormDateTime';
import FormGroup from '../FormGroup/FormGroup';
import FormImage from '../FormImage/FormImage';
import FormInput from '../FormInput/FormInput';
import FormNumInput from '../FormNumInput/FormNumInput';
import FormRadio from '../FormRadio/FormRadio';
import FormSectionsDate from '../FormSectionsDate/FormSectionsDate';
import FormSelect from '../FormSelect/FormSelect';
import FormSlider from '../FormSlider/FormSlider';
import FormTextear from '../FormTextear/FormTextear';
import FormTime from '../FormTime/FormTime';
import FormUploadImg from '../FormUploadImg/FormUploadImg';

// tslint:disable-next-line:no-any
const beforeComponentProps = {} as { [key: string]: any };

/**
 * 根据的枚举渲染不同的表单
 * 接收参数相信请看字段接口类型
 */
class FormRender extends React.Component<ISurveyHook> {
  private shouldUpdates = ['showStatus', 'errMsg'];
  private safeComponent = ['FormCount'];
  public render() {
    return this.formRender();
  }

  public shouldComponentUpdate(p: ISurveyHook) {
    // 算分组件直接刷新
    if (this.safeComponent.includes(p.formObj.subjectData.subjectType)) {
      return true;
    }
    // 复合组件
    if (p.formObj.subjectData.subjectType === 'FormGroup') {
      const isFormGropRender = [] as boolean[];
      // tslint:disable-next-line:no-any
      p.formObj.subjectData.groupChildrens!.map((item: any) => {
        // tslint:disable-next-line:no-any
        item.forEach((is: any) => {
          this.shouldUpdates.forEach(key => {
            if (beforeComponentProps[is.subjectData.field]) {
              const isGroupShow = beforeComponentProps[is.subjectData.field][key] !== is.subjectData[key];
              isFormGropRender.push(isGroupShow);
            } else {
              isFormGropRender.push(true);
            }
          });
          beforeComponentProps[is.subjectData.field] = {};
          this.shouldUpdates.forEach(key => {
            beforeComponentProps[is.subjectData.field][key] = is.subjectData[key];
          });
        });
      });
      const isGroupRender = this.isRender(p);
      return isFormGropRender.some(item => item) || isGroupRender;
    }

    const isRender = this.isRender(p);
    return isRender;
  }

  /**
   * 非复合组件判断是否进行渲染
   */
  private isRender = (p: ISurveyHook) => {
    const isRender = this.shouldUpdates.some(key => {
      return beforeComponentProps[p.formObj.subjectData.field]
        ? beforeComponentProps[p.formObj.subjectData.field][key] !== p.formObj.subjectData[key]
        : true;
    });
    // 根据字段 保存上次结果
    beforeComponentProps[this.props.formObj.subjectData.field] = {};
    this.shouldUpdates.forEach(key => {
      beforeComponentProps[this.props.formObj.subjectData.field][key] = this.props.formObj.subjectData[key];
    });
    return isRender;
  };

  /**
   * 渲染表单
   * @param { ISubjectV } // 题目信息
   */
  private formRender = () => {
    const data = this.props.formObj;
    if (data.subjectData.showStatus || data.subjectData.isGroupChildren) {
      switch (data.subjectData.subjectType) {
        case 'FormInput':
          return <FormInput formObj={data} />;
        case 'FormTextear':
          return <FormTextear formObj={data} />;
        case 'FormSlider':
          return <FormSlider formObj={data} />;
        case 'FormRadio':
          return <FormRadio formObj={data} />;
        case 'FormCheckbox':
          return <FormCheckbox formObj={data} />;
        case 'FormNumInput':
          return <FormNumInput formObj={data} />;
        case 'FormSectionsDate':
          return <FormSectionsDate formObj={data} />;
        case 'FormDate':
          return <FormDate formObj={data} />;
        case 'FormTime':
          return <FormTime formObj={data} />;
        case 'FormSelect':
          return <FormSelect formObj={data} />;
        case 'FormCount':
          return <FormCount formObj={data} />;
        case 'FormImage':
          return <FormImage formObj={data} />;
        case 'FormDateTime':
          return <FormDateTime formObj={data} />;
        case 'FormAloneDate':
          return <FormAloneDate formObj={data} />;
        case 'FormGroup':
          return <FormGroup formObj={data} />;
        case 'FormUploadImg':
          return <FormUploadImg formObj={data} />;
        default:
          return null;
      }
    }
    return null;
  };
}
export default FormRender;
